// TODO:  计划书
import React, { VFC, useState, useEffect} from "react"
import {Modal, Form, Input, Button, message, Space  } from 'antd';
import { useHistory } from "react-router-dom";;

import { saveTeamDetectPlan, getTeamDetectPlanList} from '@services/index';
import { pick} from '@tools/utils'

import NoInfo from '@components/noInfo';

const FormItem = Form.Item;
const { TextArea } = Input;

const Prospectus:VFC<any> = props => {
    const {teamId, isVisible, onCancel} = props;
    const {location} = useHistory() as any;
    const [isShow, setIsShow] = useState<boolean>(false);
    const [formInfo, setFormInfo] = useState<any>({});
    // const [loading, setLoading] = useState<boolean>(false)
    useEffect(()=>{
        init()
    }, [])
    const init = () => {
        getTeamDetectPlanList({
            pageNum:1,
            pageSize:10,
            phaseId: location.state?.phaseId,
            teamId,
            projectId: JSON.parse(sessionStorage.userInfo).projectId
        }).then(({status, data}) => {
            if(status === '0'){
                const _data = data?.list;
                let _isShow = _data.length>0?true:false;
                setIsShow(_isShow);
                setFormInfo(_data[0])
            }
        })
    }
    // 修改输入框值
    const onChange = (e:any, type:string):void => {
        setFormInfo({
            ...formInfo,
            [type]: e?.target?e.target.value:e
        })
    }
    // 提交表单数据
    const submit = async (type: string) => {
        const params = pick(formInfo, [
            "projectId",
            "phaseId",
            "teamId",
            "id",
            "comment",
            "pass"
        ]);
        if(!params.comment){
            message.error('请输入评语');
            return
        }
        // 0 未评论 1 通过 2 未通过
        params.pass = type=='Y' ? 1: 2
        const { status } = await saveTeamDetectPlan(params)
        if(status == '0'){
            message.success('提交成功');
            onCancel()
        }
    };
    return (
        <Modal title={formInfo?.teamName} visible={isVisible} onCancel={onCancel} footer={false}>
            {isShow&&<Form >
                <FormItem label="标题">
                    <div>{formInfo?.name}</div>
                </FormItem>
                <FormItem label="内容">
                    <div dangerouslySetInnerHTML={{__html: formInfo?.content}}></div>
                </FormItem>
                <FormItem label="日期">
                    <div>{formInfo?.updateTime}</div>
                </FormItem>
                <FormItem label="评语">
                    <TextArea  value={formInfo?.comment} onChange={e => onChange(e, 'comment')} placeholder="请输入评语"/>
                </FormItem>
                {formInfo&&<Space size={20} style={{display: 'flex',justifyContent: 'center'}}>
                    <Button type="primary" onClick={()=>submit('Y')}>通过</Button>
                    <Button onClick={()=>submit('N')}>不通过</Button>
                </Space>}
            </Form>
            }
            {!isShow&&<NoInfo />}
        </Modal>
    )
}
export default Prospectus